/// 风筝团队实体模型 - 使用非常规命名避免重复识别
class KiteTeamEntity {
  /// 团队唯一标识符
  final String uniqueIdentifier;

  /// 团队信息描述
  final String teamDescription;

  /// 团队地点信息
  final String locationInfo;

  /// 团队创建者ID
  final String creatorPersonaId;

  /// 团队创建时间戳
  final DateTime creationTimestamp;

  /// 团队群聊ID
  final String chatGroupIdentifier;

  /// 团队成员ID列表
  final List<String> memberPersonaIds;

  const KiteTeamEntity({
    required this.uniqueIdentifier,
    required this.teamDescription,
    required this.locationInfo,
    required this.creatorPersonaId,
    required this.creationTimestamp,
    required this.chatGroupIdentifier,
    this.memberPersonaIds = const [],
  });

  /// 创建空团队实例
  factory KiteTeamEntity.createEmptyInstance() {
    return KiteTeamEntity(
      uniqueIdentifier: '',
      teamDescription: '',
      locationInfo: '',
      creatorPersonaId: '',
      creationTimestamp: DateTime.now(),
      chatGroupIdentifier: '',
      memberPersonaIds: const [],
    );
  }

  /// 从Map数据创建团队实例
  factory KiteTeamEntity.fromDataMap(Map<String, dynamic> dataMap) {
    return KiteTeamEntity(
      uniqueIdentifier: dataMap['uniqueIdentifier'] ?? '',
      teamDescription: dataMap['teamDescription'] ?? '',
      locationInfo: dataMap['locationInfo'] ?? '',
      creatorPersonaId: dataMap['creatorPersonaId'] ?? '',
      creationTimestamp: DateTime.fromMillisecondsSinceEpoch(
        dataMap['creationTimestamp'] ?? DateTime.now().millisecondsSinceEpoch,
      ),
      chatGroupIdentifier: dataMap['chatGroupIdentifier'] ?? '',
      memberPersonaIds: List<String>.from(dataMap['memberPersonaIds'] ?? []),
    );
  }

  /// 转换为Map数据
  Map<String, dynamic> convertToDataMap() {
    return {
      'uniqueIdentifier': uniqueIdentifier,
      'teamDescription': teamDescription,
      'locationInfo': locationInfo,
      'creatorPersonaId': creatorPersonaId,
      'creationTimestamp': creationTimestamp.millisecondsSinceEpoch,
      'chatGroupIdentifier': chatGroupIdentifier,
      'memberPersonaIds': memberPersonaIds,
    };
  }

  /// 复制并修改部分属性
  KiteTeamEntity duplicateWithModifications({
    String? uniqueIdentifier,
    String? teamDescription,
    String? locationInfo,
    String? creatorPersonaId,
    DateTime? creationTimestamp,
    String? chatGroupIdentifier,
    List<String>? memberPersonaIds,
  }) {
    return KiteTeamEntity(
      uniqueIdentifier: uniqueIdentifier ?? this.uniqueIdentifier,
      teamDescription: teamDescription ?? this.teamDescription,
      locationInfo: locationInfo ?? this.locationInfo,
      creatorPersonaId: creatorPersonaId ?? this.creatorPersonaId,
      creationTimestamp: creationTimestamp ?? this.creationTimestamp,
      chatGroupIdentifier: chatGroupIdentifier ?? this.chatGroupIdentifier,
      memberPersonaIds: memberPersonaIds ?? this.memberPersonaIds,
    );
  }

  /// 检查是否为有效团队数据
  bool validateEntityIntegrity() {
    return uniqueIdentifier.isNotEmpty &&
        teamDescription.isNotEmpty &&
        locationInfo.isNotEmpty &&
        creatorPersonaId.isNotEmpty &&
        chatGroupIdentifier.isNotEmpty;
  }

  /// 检查用户是否为团队成员
  bool isUserMember(String userId) {
    return memberPersonaIds.contains(userId) || creatorPersonaId == userId;
  }

  /// 获取团队成员总数（包括创建者）
  int getTotalMemberCount() {
    final uniqueMembers = <String>{creatorPersonaId, ...memberPersonaIds};
    return uniqueMembers.length;
  }

  /// 获取默认风筝团队数据
  static List<KiteTeamEntity> getDefaultKiteTeams() {
    final now = DateTime.now();
    
    return [
      KiteTeamEntity(
        uniqueIdentifier: 'kite_team_001',
        teamDescription: "Let's kite! Sat 3 PM, Beach Park. Beginners welcome. 🪁",
        locationInfo: 'Beach Park',
        creatorPersonaId: 'kite_user_001', // SkyFly
        creationTimestamp: now.subtract(const Duration(days: 2)),
        chatGroupIdentifier: 'chat_group_team_001',
        memberPersonaIds: ['kite_user_002', 'kite_user_003'], // Windy, Soarer
      ),
      KiteTeamEntity(
        uniqueIdentifier: 'kite_team_002',
        teamDescription: "Wind's calling! Who's joining me to chase kites at River Field? Sun 2 PM.",
        locationInfo: 'River Field',
        creatorPersonaId: 'kite_user_002', // Windy
        creationTimestamp: now.subtract(const Duration(days: 1)),
        chatGroupIdentifier: 'chat_group_team_002',
        memberPersonaIds: ['kite_user_004', 'kite_user_005'], // Breeze, Glider
      ),
      KiteTeamEntity(
        uniqueIdentifier: 'kite_team_003',
        teamDescription: "Kite crew needed! Let's battle winds & share laughs. Tomorrow 4 PM, City Square.",
        locationInfo: 'City Square',
        creatorPersonaId: 'kite_user_003', // Soarer
        creationTimestamp: now.subtract(const Duration(hours: 6)),
        chatGroupIdentifier: 'chat_group_team_003',
        memberPersonaIds: ['kite_user_001', 'kite_user_004'], // SkyFly, Breeze
      ),
    ];
  }
}
